﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace WPay.Infrastructure.Unity
{
    public class XMLHelper
    {
        /// <summary>
        /// 获取返回参数
        /// </summary>
        /// <returns></returns>
        public static string GetXmlNode(string xmlresult, string key)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlresult);
            var text = xmlDocument.SelectSingleNode($"/WU_ROOT/BODY/RETURN_DATA/{key}");
            if (text == null)
            {
                return GetErrorMsg(xmlresult, key);
            }
            return text.InnerText;
        }

        private static string GetErrorMsg(string xmlresult, string key)
        {
            //正则获取结果
            var return_msg = $"(?<=(<{key}>))[.\\s\\S]*?(?=(</{key}>))";
            var return_msg_res = Regex.Match(xmlresult, return_msg).Value;
            return return_msg_res;

        }
    }
}
